package com.urbane.order.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单主表实体（Orders）
 * 功能：
 * - 存储订单核心信息：用户、金额、状态、地址、支付方式等
 * - 关联多个 OrderItem（明细）
 * <p>
 * 数据库表：orders
 * <p>
 * 注意：
 * - 所有金额使用 BigDecimal，避免浮点误差
 * - status 字段使用枚举，防止非法值
 * - create_time 和 update_time 自动维护
 */
@Data
@Entity
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "order_no", unique = true, nullable = false, length = 50)
    private String orderNo; // 订单号，如 ORD20250405123456

    @Column(name = "user_id", nullable = false)
    private Long userId; // 下单用户 ID

    @Column(name = "total_amount", precision = 10, scale = 2, nullable = false)
    private BigDecimal totalAmount; // 订单总金额（含运费）

    @Column(name = "pay_amount", precision = 10, scale = 2)
    private BigDecimal payAmount; // 实际支付金额（可能小于 totalAmount）

    @Column(name = "freight", precision = 10, scale = 2, nullable = false)
    private BigDecimal freight; // 运费

    @Column(name = "discount_amount", precision = 10, scale = 2)
    private BigDecimal discountAmount; // 优惠金额（券+积分）

    @Column(name = "status", nullable = false, length = 20)
    private OrderStatus status = OrderStatus.PENDING_PAYMENT; // 订单状态

    @Column(name = "payment_method", length = 20)
    private PaymentMethod paymentMethod; // 支付方式

    @Column(name = "address_id", nullable = false)
    private Long addressId; // 收货地址 ID

    @Column(name = "receiver_name", length = 50, nullable = false)
    private String receiverName;

    @Column(name = "receiver_phone", length = 20, nullable = false)
    private String receiverPhone;

    @Column(name = "receiver_province", length = 50, nullable = false)
    private String receiverProvince;

    @Column(name = "receiver_city", length = 50, nullable = false)
    private String receiverCity;

    @Column(name = "receiver_district", length = 50, nullable = false)
    private String receiverDistrict;

    @Column(name = "receiver_detail", length = 200, nullable = false)
    private String receiverDetail;

    @Column(name = "remark", length = 500)
    private String remark; // 用户备注

    @Column(name = "coupon_id")
    private Long couponId; // 使用的优惠券 ID

    @Column(name = "used_points")
    private Integer usedPoints; // 使用的积分数量

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "GMT+8")
    private LocalDateTime createdAt;

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "GMT+8")
    private LocalDateTime updatedAt;

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "GMT+8")
    private LocalDateTime paidAt; // 支付时间

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "GMT+8")
    private LocalDateTime cancelledAt; // 取消时间

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "GMT+8")
    private LocalDateTime shippedAt; // 发货时间

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "GMT+8")
    private LocalDateTime deliveredAt; // 签收时间

    // ========== 构造函数 ==========
    public Order() {
    }

    public Order(String orderNo, Long userId, BigDecimal totalAmount, BigDecimal freight,
                 Long addressId, String receiverName, String receiverPhone,
                 String receiverProvince, String receiverCity, String receiverDistrict,
                 String receiverDetail) {
        this.orderNo = orderNo;
        this.userId = userId;
        this.totalAmount = totalAmount;
        this.freight = freight;
        this.addressId = addressId;
        this.receiverName = receiverName;
        this.receiverPhone = receiverPhone;
        this.receiverProvince = receiverProvince;
        this.receiverCity = receiverCity;
        this.receiverDistrict = receiverDistrict;
        this.receiverDetail = receiverDetail;
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }
}
